package com.ms.game.slot.base;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

import com.ms.service.db.jedis.JedisManager;
import com.xpec.c4.game.time.TimeSys;

public class SlotWaterDisappearRedisSys 
{
	private static final String WATER_REDIS_DBKEY ="waterdisappear"; 
	
	private static final  int WAIT_TIME = 10;
	
	
	private static ConcurrentHashMap<String,Long> checkFlagList = new ConcurrentHashMap<String,Long>();
	
	/**
	 * 是否有清除標記
	 * @return
	 */
	public static boolean hasFlag(String halltype , String seatid)
	{
		String key =getKey(halltype,seatid);
		long nowtime = TimeSys.currentTimeSec();
		long time =nowtime;
		if(!checkFlagList.containsKey(key))
		{
			checkFlagList.put(key, nowtime);
		}
		else
		{
			time = checkFlagList.get(key);
		}
		
		//1500532955
		if(time > TimeSys.currentTimeSec())
		{
			return false;
		}
		
		//更新下次檢查時間
		checkFlagList.put(key,TimeSys.currentTimeSec()+WAIT_TIME);
		
		//檢查
		Jedis jedis =getJedis();
		boolean rs =  jedis.exists(key);
		JedisManager.returnResource(WATER_REDIS_DBKEY, jedis);
		
		return rs;
	}
	
	/**
	 * 移除清除標記
	 * @return
	 */
	public static void delFlag(String halltype , String seatid)
	{
		String key =getKey(halltype,seatid);
		Jedis jedis = getJedis();
		jedis.del(key);
	}
	
	
	public static String getKey(String halltype , String seatid)
	{
		return  halltype +"_"+seatid;
	}
	
	/**
	 * 下一次檢查時間
	 * @return
	 */
	public static long nextCheckTime()
	{
		return TimeSys.currentTimeSec() + WAIT_TIME;
	}

	/**
	 * 讀取pipeline - 批量塞入
	 * @return
	 */
	public static void setFlagToRedis(String halltype , ArrayList<String> data)
	{
		Jedis jedis = getJedis();
		Pipeline pp =  jedis.pipelined();
		for(String seatid:data)
		{
			pp.set(SlotWaterDisappearRedisSys.getKey(halltype, seatid),"1");
		}
		pp.sync();
		
		JedisManager.returnResource(WATER_REDIS_DBKEY, jedis);
		data.clear();
	}
	
	
	
	
	private static Jedis getJedis()
	{
		return JedisManager.getResource(WATER_REDIS_DBKEY);
	}
}
